package logicBusiness;

import DAO.DiaDao;
import models.Dia;
import java.util.List;
import javax.persistence.*;

public class DiaBL {

    private int ok = 0;
    private Dia d = new Dia();

    public int crearDia(String nombreDia) {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        DiaDao dao = new DiaDao(em);
        em.getTransaction().begin();
        d = dao.crearDia(nombreDia);
        em.getTransaction().commit();
        em.close(); //quitar cuando se tenga montado el serv
        emf.close();//quitar cuando se tenga montado el serv
        if (d == null) {
            ok = 0;
        } else if (d.getNombreDia().equals(nombreDia)) {
            ok = 1;
        }
        return ok;

    }

    public void borrarDia(String nombreDia) {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        DiaDao dao = new DiaDao(em);
        em.getTransaction().begin();
        dao.borrarDia(nombreDia);
        em.getTransaction().commit();
        em.close();
        emf.close();
    }

    public int encontrarDia(String nombreDia) {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        DiaDao dao = new DiaDao(em);
        d = dao.encontrarDia(nombreDia);
        em.close();
        emf.close();
        if (d == null) {
            ok = 0;
        } else if (d.getNombreDia().equals(nombreDia)) {
            ok = 1;
        }
        return ok;
    }

    public Dia consultarDia(String nombreDia) {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        DiaDao dao = new DiaDao(em);
        d = dao.encontrarDia(nombreDia);
        em.close();
        emf.close();
        return d;
    }

    public int actualizarDia(String nombreDia) {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        DiaDao dao = new DiaDao(em);
        em.getTransaction().begin();
        d = dao.actualizarDia(nombreDia);
        em.getTransaction().commit();
        em.close();
        emf.close();
        if (d == null) {
            ok = 0;
        } else if (d.getNombreDia().equals(nombreDia)) {
            ok = 1;
        }
        return ok;
    }

    public List consultarDias() {
        List results;
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        DiaDao dao = new DiaDao(em);
        results = dao.encontrarDias();
        em.close();
        emf.close();
        return results;
    }
}
